package com.scqzy.shengchunqiang.sword2offer.day07;

import com.scqzy.shengchunqiang.structure.TreeNode;

import java.util.Objects;

/**
 * @Description: 剑指 Offer 28. 对称的二叉树
 * @Author 盛春强
 * @Date 2021/8/14 20:59
 */
public class IsSymmetric {
    public boolean isSymmetric(TreeNode root) {
        if (Objects.isNull(root)) {
            return true;
        }
        return isSym(root.left,root.right);
    }

    private boolean isSym(TreeNode left, TreeNode right) {
        if (Objects.isNull(left) && Objects.isNull(right)) {
            return true;
        }
        if (Objects.isNull(left) || Objects.isNull(right) || left.val != right.val) {
            return false;
        }
        return isSym(left.left,right.right) && isSym(left.right, right.left);
    }
}
